//
//  YDMinePositionController.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2017/11/13.
//  Copyright © 2017年 LK-IOS. All rights reserved.
//

#import "YDMinePositionController.h"
#import "YDMinePositionTopView.h"
#import "YDMinePositionBottomView.h"
#import "YDRechargeTopView.h"
#import "YDHoldPositionModel.h"

@interface YDMinePositionController ()
//YDMinePositionTopView
@property (nonatomic , weak) YDRechargeTopView *topView;
//YDMinePositionBottomView
@property (nonatomic , weak) YDMinePositionBottomView *bottomView;


@property (nonatomic, strong)YDHoldPositionModel *holdPositionModel;

//引导提示view
@property (nonatomic, strong)SYGuideLoginView *guideLoginView;


@end

@implementation YDMinePositionController

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self setupNav];
    [self topView];
    if ([SYUserInformation sharedInstance].isLogin)
    {
        [self loadData];
    }else
    {
        [self guideLoginView];
    }

    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
   
}



- (void)setupNav
{
    self.view.backgroundColor = [UIColor colorWithHexString:mainGray];
    self.title = @"我的持仓";
    [self.navigationController.navigationBar setHidden:YES];
}
-(void)addChildViews
{
    [self topView];
    [self bottomView];
}

- (void)loadData
{
    [[SYProgressHUD sharedInstance] showMBHudWithSting:kLoadingText];
    [BaseNetRequest sessionRequestWithparameters:nil method:GETMethod requstType:CapitalAccountInfo WithSuccessBlock:^(NSDictionary *resultDict) {
        [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
        
        if ([resultDict[@"code"] integerValue]== 200) {
            self.holdPositionModel = [YDHoldPositionModel mj_objectWithKeyValues:resultDict[@"result"]];
            [self addChildViews];
            self.topView.amountString = self.holdPositionModel.pzHoldProfitOrLoss;
        }else
        {
            [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:resultDict[@"message"] isExistNav:NO];
            
        }
        
    } failBlock:^(NSError *error) {
        [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
        [[NavTipsView defaultTipsView] showTipsBannerInView:self.view WithType:NavTipsViewTypeMessage text:kNet_Error_ConnectionLost isExistNav:NO];
    }];
}

//YDMinePositionTopView
//-(YDMinePositionTopView *)topView
//{
//    if (_topView == nil) {
//        YDMinePositionTopView *topView = [YDMinePositionTopView minePositionTopViewWithModel:self.holdPositionModel];
//        [self.view addSubview:topView];
//        [topView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.right.top.equalTo(self.view);
//            if (isPhoneX) {
//                make.height.equalTo(@(rateX812(130)));
//            }else{
//                make.height.equalTo(@(rate750(130)));
//            }
//        }];
//
//        _topView = topView;
//    }
//    return _topView;
//}

////*******YDRechargeTopView********////
-(YDRechargeTopView *)topView
{
    if (_topView == nil) {
      
        YDRechargeTopView *topView = [YDRechargeTopView rechargeTopViewWithAmount:nil];
        __weak typeof(self)weself = self;
        topView.title = @"我的持仓";
        topView.name = @"持仓盈亏（元）";
        [topView setBackBlock:^(BOOL isSelected) {
            [weself.navigationController popViewControllerAnimated:YES];
        }];
        [self.view addSubview:topView];
        [topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self.view);
            if (isPhoneX) {
                make.height.equalTo(@(rateX812(170) + 24));
            }else{
                make.height.equalTo(@(rate750(170)));
            }
        }];
        _topView = topView;
    }
    return _topView;
}

-(YDMinePositionBottomView *)bottomView
{
    if (_bottomView == nil) {
        YDMinePositionBottomView *bottomView = [YDMinePositionBottomView minePositionBottomViewWithModel:self.holdPositionModel];
        [self.view addSubview:bottomView];
        [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.topView.mas_bottom);
            if (isPhoneX) {
                make.height.equalTo(@(rateX812(136)));
            }else{
                make.height.equalTo(@(rate750(136)));
            }
        }];
        _bottomView = bottomView;
    }
    return _bottomView;
}
-(SYGuideLoginView *)guideLoginView
{
    if (!_guideLoginView) {
        if (![SYUserInformation sharedInstance].isLogin) {
            //没有登录贴引导登录展位图
            
            _guideLoginView = [[SYGuideLoginView alloc] init];
            [self.view addSubview:_guideLoginView];
            [_guideLoginView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.equalTo(self.view);
                make.top.equalTo(isPhoneX ? @(rateX812(40)) : @(rate750(40)));
            }];
            __weak typeof(self)weself = self;
            [_guideLoginView setClickBlock:^(BOOL isClick) {
                YDLoginController *loginVC = [[YDLoginController alloc] init];
                loginVC.loginStyle = Login_Present;
                YDBaseNavigationController *baseNav = [[YDBaseNavigationController alloc] initWithRootViewController:loginVC];
                [weself presentViewController:baseNav animated:YES completion:nil];
            }];
        }
    }
    return _guideLoginView;
}
@end
